#!/bin/sh
#
# iscsid iSCSI daemon
#
# chkconfig: 345 7 89
# description: Starts and stops the iSCSI daemon.
#
# processname: iscsid
# pidfile: /var/run/iscsid.pid
# config:  /etc/iscsi/iscsid.conf

### BEGIN INIT INFO
# Provides:          iscsid
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts and stops login iSCSI daemon.
# Description: iscsid provides the iSCSI session and connection state machine
#              for software iscsi/iser (iscsi_tcp/ib_iser) and partialy
#              offloaded hardware (bnx2i).
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec=/sbin/iscsid
prog=iscsid
config=/etc/iscsi/iscsid.conf
lockfile=/var/lock/subsys/$prog
iscsi_lockfile=/var/lock/subsys/iscsi

# FIXME this has a false positive for root on nfs
root_is_iscsi() {
    rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
    [[ "$rootopts" =~ "_netdev" ]]
}

start_iscsid() {
    echo -n $"Starting $prog: "
    modprobe -q iscsi_tcp
    modprobe -q ib_iser
    modprobe -q cxgb3i
    modprobe -q cxgb4i
    modprobe -q bnx2i
    modprobe -q be2iscsi
    daemon iscsiuio
    daemon $prog
    retval=$?
    echo
    touch $lockfile
}

force_start() {
    start_iscsid
    # a force start could imply the iscsi service is started due to how it
    # lazy starts. We need to touch the lock file so it is shutdown later
    touch $iscsi_lockfile
}

use_discoveryd() {
    grep -qrs "discovery.sendtargets.use_discoveryd = Yes" /var/lib/iscsi/send_targets
    if [ $? -eq 0 ] ; then
 	return 0
    fi

    grep -qrs "discovery.isns.use_discoveryd = Yes" /var/lib/iscsi/isns
    if [ $? -eq 0 ] ; then
 	return 0
    fi

    return 1
}

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6

    # only start if nodes are setup to startup automatically, root is iscsi,
    # or if iscsid is managing the sessions.
    grep -qrs "node.startup = automatic" /var/lib/iscsi/nodes
    if [ $? -eq 0 ] || root_is_iscsi || use_discoveryd ; then
        start_iscsid 
        return $?
    fi

    return 0
}

stop() {
    if use_discoveryd ; then
	iscsiadm -k 0 2>/dev/null
    fi

    declare -a iparams=( $(iscsiadm -m session 2>/dev/null | egrep "tcp|iser|bnx2i|cxgb3i|cxgb4i|be2iscsi") )
    if [[ -n "${iparams[*]}" ]]; then
        # We have active sessions, so don't stop iscsid!!
        echo -n $"Not stopping $prog: iscsi sessions still active"
        warning $"Not stopping $prog: iscsi sessions still active"
        echo
        return 0
    fi

    echo -n $"Stopping $prog: "
 
    iscsiadm -k 0 2>/dev/null
    echo

    killproc iscsiuio
    rm -f /var/run/iscsiuio.pid

    # only remove the iscsi drivers when offload is used
    rmmod bnx2i 2>/dev/null
    rmmod cnic 2>/dev/null

    rmmod cxgb3i 2>/dev/null
    rmmod cxgb4i 2>/dev/null

    # a bug in kobject netlink code will cause this to oops
    # modprobe -r be2iscsi 2>/dev/null

    modprobe -r ib_iser 2>/dev/null
    modprobe -r iscsi_tcp 2>/dev/null

    rm -f $lockfile
    return 0
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

restart() {
    rh_status_q
    use_force_start=$?

    stop
    # if iscsid was running then make sure it starts up
    if [ "$use_force_start" -eq 0 ] ; then
	start_iscsid
    else
	start
    fi
}

reload() {
    return 3
}

force_reload() {
    restart
}

rh_status() {
    status $prog
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    force-start)
        force_start
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0
{start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
